<?php
class ContactsController extends LilcontactsAppController {

	var $name = 'Contacts';
	var $helpers = array('Html', 'Form');
	
	function index() {
		$params = array();
		$params['kind'] = strtoupper(@$this->params['named']['kind'])=='C'?'C':'T';
		if (!empty($this->data['search'])) $params['search'] = $this->data['search'];
		$params = array_merge($this->params['named'], $params);
		
		$this->paginate = array(
			'contain' 		=> array('Email', 'Phone', 'Address', 'Company'),
			'conditions' 	=> $this->Contact->filter($params),
			'order' 		=> 'Contact.title',
		);
		$data = $this->paginate('Contact');
		
		if (sizeof($data)==1 && !empty($params['search'])) {
			$this->redirect(array('action'=>'view', $data[0]['Contact']['id']));
		}
		
		$this->set('contacts', $data);
		$this->set('params', $params);
		//$this->set('sidebar', '../contacts/sidebar/index');
	}
	
	function add() {
		$this->setAction('edit');
	}
	
	function edit($id=null) {
		if (!empty($this->data)) {
			$this->Contact->create();
			
			// find company it it already exists
			if (empty($this->data['Company']['title'])) {
				unset($this->data['Company']); 
			} else {
				$this->data['Company']['id'] = $this->Contact->field(
					'id', array('kind'=>'C', 'title'=>$this->data['Company']['title'])
				);
				$this->data['Company']['kind'] = 'C';
			}
			
			// create contact's "display title" from name and surname
			if ($this->data['Contact']['kind'] == 'T' && 
				isset($this->data['Contact']['name']) && 
				isset($this->data['Contact']['surname'])) 
			{
				$this->data['Contact']['title'] = implode(' ', Set::filter(
					array($this->data['Contact']['name'], $this->data['Contact']['surname'])
				));
			}
			
			if ($this->Contact->saveAll($this->data)) {
				$this->Session->setFlash(__('Contact has been successfully saved.', true));
				
				$referer = trim(base64_decode(@$this->data['Contact']['referer']));
				if (!empty($referer) && $referer!='?') {
					$this->redirect(array_merge(
						$this->parseUrl($referer), array('highlight'=>$this->Contact->id)
					));
				} else {
					$this->redirect(array('action'=>'view', $this->Contact->id));
				}
			} else {
				$this->Session->setFlash(__('There are some errors in the form. '.
					'Please correct all marked fields below.', true), 'error');
			}
		} else {
			if (is_numeric($id)) {
				$this->Contact->contain('Company'); 
				if (!$this->data = $this->Contact->read(null, $id)) {
					$this->error404();
				}
			} else if (!is_null($id)) {
				$this->error404();
			} else {
				if (isset($this->params['named']['kind']) && 
					in_array(strtoupper($this->params['named']['kind']), array('C', 'T'))) 
				{
					$this->data['Contact']['kind'] = strtoupper($this->params['named']['kind']);
				}
			}
			$this->data['Contact']['referer'] = base64_encode($this->referer('?'));
		}
	}

	function view($id=null) {
		$this->Contact->contain(array('Email', 'Phone', 'Address', 'Company'));
		if (is_numeric($id) && $data=$this->Contact->read(null, $id)) {
			// set referer for add tag form
			$this->set('referer', base64_encode(Router::url(null, true)));
			
			$this->set('contact', $data);
			$this->set('sidebar', 'contacts/view');
		} else {
			$this->error404();
		}
	}
	
	function delete($id = null) {
		if (!is_numeric($id) || !$this->Contact->del($id)) {
			$this->error404();
		} else {
			if (!$this->RequestHandler->isAjax()) {
				$this->Session->setFlash(__('Contact has been successfully deleted.', true));
			}
			$this->redirect($this->referer());
		}
	}

}
?>